import opts
import json
import numpy as np

if __name__ == '__main__':
    opt = opts.parse_opt()
    info = json.load(open(opt.input_json))
    ix_to_word = info['ix_to_word']
    word_to_ix = dict()
    seen = 0

    for k, v in ix_to_word.items():
        word_to_ix[v] = k
    vocab = np.random.uniform(-0.1, 0.1, (len(word_to_ix) + 1, 300))
    
    with open(opt.pretrained_glove) as f_in:
        for line in f_in:
            if line != "":
                splitline = line.split()
                word = splitline[0]
                embedding = splitline[1:]
                if word in word_to_ix and len(embedding) == 300:
                    temp = np.array([float(val) for val in embedding])
                    vocab[int(word_to_ix[word]), :] = temp
                    seen += 1

    vocab = vocab.astype(np.float)
    np.save("/mnt/hdd0/home/fyc/Transformer/data/glove/word_emb_add_rel", vocab)
    print('see number', seen)

